package com.rgp.serviceprice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rgp.internalcommon.common.CommonStatusEnum;
import com.rgp.internalcommon.dto.OrderPriceDto;
import com.rgp.internalcommon.dto.ResponseResult;
import com.rgp.serviceprice.utils.TollRulePriceCalculateUtils;
import com.rgp.serviceprice.mapper.TollRuleMapper;
import com.rgp.serviceprice.pojo.TollRule;
import com.rgp.serviceprice.service.CalculatePriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author nhp
 * @version 1.0
 * @description TODO
 * @date 2024-04-26 18:26
 */
@Service
public class CalculatePriceServiceImpl implements CalculatePriceService {

    @Autowired
    private TollRuleMapper tollRuleMapper;

    @Override
    public ResponseResult<BigDecimal> calculateOrderPrice(OrderPriceDto orderPriceDto) {

        //1.获取材料
        String cityCode = orderPriceDto.getCityCode();
        String vehicleType = orderPriceDto.getVehicleType();
        Long distance = orderPriceDto.getDistance();
        Long duration = orderPriceDto.getDuration();

        //2.根据乘客所在城市的cityCode 与 所选载具的类型 vehicleType来查询最新版本的计价规则
        String fareType = cityCode + "$" + vehicleType;
        QueryWrapper<TollRule> wrapper = new QueryWrapper<>();
        wrapper.eq("fare_type",fareType);
        wrapper.orderByDesc("fare_version");
        List<TollRule> tollRules = tollRuleMapper.selectList(wrapper);
        //获取当前fareType下的最新版本的计价规则
        if (tollRules == null || tollRules.size() == 0){
            return ResponseResult.fail(CommonStatusEnum.TOLL_RULE_NOT_EXIST);
        }
        TollRule tollRule = tollRules.get(0);

        //3.计算订单价格
        String price = TollRulePriceCalculateUtils.calculatePrice(tollRule, distance + "", duration + "");

        return ResponseResult.success(new BigDecimal(price));
    }
}